-module(common).
-include("yaws_api.hrl").
-include("rec_dec.hrl").
-export([header/0,header/1,include_js/2,include_css/2,json_process_arg/1,html_process_arg/1,get_session_rec/1]).
-define(JS_PATH,"/js/").
-define(CSS_PATH,"/css/").
-define(THIRDPARTY_PATH,"/3rdparty").

header()->
	header(release).
header(release) -> 
	[js_header()|css_header()];
header(debug) ->
	[js_header(debug)|css_header()].
	
js_header() ->
	[
		{script,[{lang,javascript},{src,lists:concat([?THIRDPARTY_PATH,?JS_PATH,"jquery-1.6.2.min.js"])}],[]},
		{script,[{lang,javascript},{src,lists:concat([?THIRDPARTY_PATH,?JS_PATH,"jquery-ui-1.8.16.custom.min.js"])}],[]},
		{script,[{lang,javascript},{src,lists:concat([?THIRDPARTY_PATH,?JS_PATH,"jquery.form.js"])}],[]},
		{script,[{lang,javascript},{src,lists:concat([?THIRDPARTY_PATH,?JS_PATH,"jquery.confirm-1.3.js"])}],[]},
		{script,[{lang,javascript},{src,lists:concat([?THIRDPARTY_PATH,?JS_PATH,"jquery.validate.js"])}],[]},
		{script,[{lang,javascript},{src,lists:concat([?THIRDPARTY_PATH,?JS_PATH,"sprintf.js"])}],[]},
        {script,[{lang,javascript},{src,lists:concat([?JS_PATH,"ebiz_xy.js"])}],[]},
		{script,[{lang,javascript},{src,lists:concat([?THIRDPARTY_PATH,?JS_PATH,"jquery.confirm-1.3.js"])}],[]},
		{script,[{lang,javascript},{src,lists:concat([?JS_PATH,"dynamic_panel.js"])}],[]},
		{script,[{lang,javascript},{src,lists:concat([?JS_PATH,"user_mgr1.js"])}],[]}
	].
js_header(debug) ->
	[
		{script,[{lang,javascript},{src,lists:concat([?THIRDPARTY_PATH,?JS_PATH,"jquery-1.6.2.js"])}],[]},
		{script,[{lang,javascript},{src,lists:concat([?THIRDPARTY_PATH,?JS_PATH,"jquery-ui-1.8.16.custom.js"])}],[]},
		{script,[{lang,javascript},{src,lists:concat([?THIRDPARTY_PATH,?JS_PATH,"jquery.form.js"])}],[]},
        {script,[{lang,javascript},{src,lists:concat([?JS_PATH,"ebiz_xy.js"])}],[]},
		{script,[{lang,javascript},{src,lists:concat([?THIRDPARTY_PATH,?JS_PATH,"jquery.confirm-1.3.js"])}],[]},
		{script,[{lang,javascript},{src,lists:concat([?JS_PATH,"dynamic_panel.js"])}],[]},
		{script,[{lang,javascript},{src,lists:concat([?JS_PATH,"user_mgr1.js"])}],[]}
	].
css_header() ->
	[
		{link,[{rel,"stylesheet"},{href,lists:concat([?THIRDPARTY_PATH,?CSS_PATH,"ui-lightness/jquery-ui-1.8.16.custom.css"])}],[]},
		{link,[{rel,"stylesheet"},{href,lists:concat([?CSS_PATH,"biz_queue.css"])}],[]}
	].

include_js(thirdparty, File) ->
		{script,[{lang,javascript},{src,lists:concat([?THIRDPARTY_PATH,?JS_PATH,File])}]};
include_js(private,File) ->
		{script,[{lang,javascript},{src,lists:concat([?JS_PATH,File])}]}.
include_css(thirdparty,File) ->
	{link,[{rel,"stylesheet"},{href,lists:concat([?THIRDPARTY_PATH,?CSS_PATH,File])}],[]};
include_css(private,File) ->
	{link,[{rel,"stylesheet"},{href,lists:concat([?CSS_PATH,File])}],[]}.

get_session_rec(Arg) ->
	(Arg#arg.opaque).
	
html_process_arg(Arg) ->
	io:format("arg cookie:~p~n",[(Arg#arg.opaque)]),
	L = yaws_api:parse_query(Arg),
	lists:append([
		{"_inter_session_id",(Arg#arg.opaque)}],
		L).
		
json_process_arg(Arg) ->
	Json_Data = case Arg#arg.clidata of
				undefined ->
					undefined;
				_ ->
					io:format("~p~n",[Arg#arg.clidata]),
					case rfc4627:decode(Arg#arg.clidata) of
					{error,_} ->
						undefined;
					{ok,Res,_} ->
						Res
					end
				end,
	lists:append([
					{"json_data",Json_Data}
					],
				 html_process_arg(Arg)).